/**
 * Created by sunyan on 2017/8/14.
 */
const webpage = require('webpage')

var width = 1800;
var height = 6500;

//时间
var today = new Date();
var year = today.getFullYear();    //年
var month = today.getMonth() + 1;  //月
var day = today.getDate();         //日
var week = today.getDay()          //周
var hour = today.getHours()        //时
var minutes = today.getMinutes()   //分

var page = webpage.create();
    page.viewportSize = {width: width, height: height};
dateObj = new dateObj()
function screen_shot_2() {
  var dateRange = defaultConfig().dateRange
  var endDate = defaultConfig().endDate
  page.open('http://121.42.252.26/operation/pages/user-login.html?dateRange='+dateRange+'&endDate='+endDate, function(status) {  //打开登陆
    console.log("Status: " + status);
    if(status === "success") {
      page.evaluate(function(w, h) {
        //设置body大小
        document.body.style.width = w + "px";
        document.body.style.height = h + "px";
        //模拟登陆
        document.getElementById('userName').value = 'liuxi'
        document.getElementById('password').value = 'yxb123YXB'
        document.getElementsByClassName('toTrendData')[0].click()
      })

      //延迟截图
      setTimeout(function () {
        console.log('截图')
        page.clipRect = { top: 170, left: 273, width: width, height: height };
        page.render('chart_images/chart_report_trend_'+ (defaultConfig().fileName || day)+'.png');
        phantom.exit();
      }, 20000);
    }
  })
  page.onUrlChanged = function(targetUrl) {
    console.log('New URL: ' + targetUrl);
  };
}
screen_shot_2()


/*
* @getLastWeekTime 获取一周以前的日期
* @getLastMonthTime 获取上个月的日期
* */
function dateObj(){
  this.getLastWeekTime = function(){
    var current=today.getTime();
    var minusDate=1000*60*60*24*7;
    var time = new Date(current-minusDate);
    var yearW = time.getFullYear(),
      monthW = time.getMonth()+1,
      dayW = time.getDate()
    var dateStr = yearW +'-'+ (monthW>=10?monthW:'0'+monthW)+'-'+dayW
    var obj = {
      year: yearW,
      month:monthW,
      day:dayW,
      dateStr:dateStr
    }
    return obj
  }
  this.getLastMonthTime = function () {  //获取上个月第一天和最后一天
    var yearM = today.getFullYear();
    var monthM = today.getMonth();
    if (monthM==0) {
      monthM=12;
      yearM=yearM-1;
    }
    if (monthM < 10) {
      monthM = "0" + monthM;
    }
    var firstDay = yearM + "-" + monthM + "-" + "01"; //上个月的第一天
    var lastDay = year + "-" + (month>=10?month:'0'+month) + "-" + '01'; //本月一天
    var obj = {
      year:yearM,
      month:monthM,
      firstDay:firstDay,
      lastDay:lastDay,
    }
    return obj
  }
}
/*
 * 基本配置
 * @fileName:截图文件名
 * @beginDate:页面应传参数: 开始时间
 * @endDate:页面应传参数: 结束时间
 * 趋势统计图：日报发周趋势图，周报发月趋势图，以此类推
 * */
function defaultConfig() {
  var fileName  = day; //截图文件名
  var shotHour = 8;   //开始截图的小时
  var dateRange = 'week' //页面应传参数: 范围标志
  var endDate = year+'-'+(month>=10?month:'0'+month)+'-'+(day>=10?day:'0'+day)//页面应传参数: 结束时间
  if(hour == shotHour && minutes >= 0){
    fileName = 'day'
    dateRange = 'week'
  }
  if(week == 5 && hour == shotHour && minutes >= 10) {
    fileName  = 'week'
    dateRange = 'month'
  }
  if(day  == 1 && hour == shotHour && minutes >= 20) {
    fileName  = 'month'
    dateRange = 'halfYear'
    endDate = year+'-'+(month>=10?month:'0'+month)
  }
  if(month == 6 && day == 1 && hour == shotHour && minutes >= 30) {
    fileName = 'firstHalf'
    dateRange = 'year'
    endDate = year+'-'+(month>=10?month:'0'+month)
  }
  if(month == 1 && day == 1 && hour == shotHour && minutes >= 40) {
    fileName = 'secondHalf'
    dateRange = 'year'
    endDate = year+'-'+(month>=10?month:'0'+month)
  }
  // if(month == 1 && day == 1 && hour == shotHour && minutes >= 50) {
  //   fileName = 'year'
  //   dateRange = 'year'
  // }
  var obj = {
    fileName:fileName,
    dateRange:dateRange,
    endDate:endDate
  }
  return obj
}
